package org.lendoo.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.lendoo.web.entity.User;
import org.lendoo.web.pojo.common.ServiceResult;
import org.lendoo.web.entity.WeChatJscode2SessionLog;
import org.lendoo.web.pojo.vo.Jscode2sessionVo;
import org.lendoo.web.thread.SaveOrUpdateUserThread;
import org.lendoo.web.util.HttpUtils;
import org.springframework.stereotype.Service;
import org.lendoo.web.enumeration.ServiceResultEnum;
import org.lendoo.web.service.LoginService;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 登录接口的实现类
 */
@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private ServiceHelper serviceHelper;

    /**
     * 根据前端给定微信登录的code获取openid
     * @param jscode2sessionVo
     * @return
     */
    @Override
    public ServiceResult<WeChatJscode2SessionLog> jscode2session(Jscode2sessionVo jscode2sessionVo) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="
                + serviceHelper.getWechatValue().getAppId() + "&secret=" + serviceHelper.getWechatValue().getAppSecret()
                + "&js_code=" + jscode2sessionVo.getCode() + "&grant_type=authorization_code";
        String result = HttpUtils.doGet(url);
        WeChatJscode2SessionLog weChatJscode2SessionLog = JSON.parseObject(result, WeChatJscode2SessionLog.class);
        weChatJscode2SessionLog.setJsCode(jscode2sessionVo.getCode());

        ServiceResult<WeChatJscode2SessionLog> serviceResult = new ServiceResult<>();
        if (null == weChatJscode2SessionLog.getErrcode()) {
            // 认证成功

            // 插入或更新User对象
            User user = new User();
            user.setOpenid(weChatJscode2SessionLog.getOpenid());
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getOpenid, user.getOpenid());
            User theUser = serviceHelper.getUserMapper().selectOne(lambdaQueryWrapper);
            if (null != theUser){
                // 更新User对象
                user.setId(theUser.getId());
                serviceHelper.getUserMapper().updateById(user);
            } else {
                // 插入User对象
                user.setAvatarUrl(jscode2sessionVo.getAvatarUrl());
                user.setCreateTime(new Date());
                serviceHelper.getUserMapper().insert(user);
            }
//            SaveOrUpdateUserThread saveOrUpdateUserThread = new SaveOrUpdateUserThread(user, serviceHelper.getUserMapper());
//            serviceHelper.getThreadPoolManager().submit(saveOrUpdateUserThread);

            weChatJscode2SessionLog.setUserId(user.getId());
            weChatJscode2SessionLog.setUserAvatarUrl(jscode2sessionVo.getAvatarUrl());
            serviceResult.setSuccess(Boolean.TRUE);
            serviceResult.setCode(ServiceResultEnum.SELECT_SUCCESS.getCode());
            serviceResult.setMessage(ServiceResultEnum.SELECT_SUCCESS.getMessage());
        } else {
            // 认证失败
            serviceResult.setSuccess(Boolean.FALSE);
            serviceResult.setCode(ServiceResultEnum.SELECT_FAIL.getCode());
            serviceResult.setMessage(ServiceResultEnum.SELECT_FAIL.getMessage());
        }
        serviceResult.setResult(weChatJscode2SessionLog);

        return serviceResult;
    }
}
